package com.softer.chengxin.dicinfo.service.impl;

import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.JsonResult;
import com.softer.chengxin.common.domin.ManagerPageResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.dicinfo.dao.DicTypeDao;
import com.softer.chengxin.dicinfo.entity.DicType;
import com.softer.chengxin.dicinfo.service.DicTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/10/13
 */
@Service
public class DicTypeServiceImpl implements DicTypeService {
    @Autowired
    private DicTypeDao dicTypeDao;

    @Override
    public ManagerPageResult getData(Page page,DicType dicType) {
        ManagerPageResult mr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        int count = dicTypeDao.getCount();
        if(count > 0){
            page.setQueryBean(dicType);
            List<DicType> list = dicTypeDao.getList(page);
            jr.setDs(list);
            mr.setCount(count);
            mr.setJson(jr);
        }
        return mr;
    }

    @Override
    public CommonResult add(DicType dicType) {
        String code = "1000";
        String maxCode = dicTypeDao.getMaxCode();
        if(StringUtils.isNotBlank(maxCode) && Integer.parseInt(maxCode)>=Integer.parseInt(code)){
            code = String.valueOf(Integer.parseInt(maxCode)+1);
        }
        dicType.setCode(code);
        int i = dicTypeDao.insert(dicType);
        if(i == 0){
            return new CommonResult(ResultEnum.FAIL.getCode(), "添加失败，请稍后重试！");
        }
        return new CommonResult(ResultEnum.SUCCESS.getCode(), "添加成功！");
    }

    @Override
    public CommonResult update(DicType dicType) {
        int i = dicTypeDao.update(dicType);
        if(i == 0){
            return new CommonResult(ResultEnum.FAIL.getCode(), "修改失败，请稍后重试！");
        }
        return new CommonResult(ResultEnum.SUCCESS.getCode(), "修改成功！");
    }

    @Override
    public DicType getDicTypeById(String id) {
        return dicTypeDao.getDicTypeById(id);
    }

}
